package com.hcb.micro.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Mall;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IManagerService;
import com.hcb.micro.util.CommonUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("manager")
public class ManagerController extends BaseControllers{

	@Autowired
	IMallService mallService;
	
	@Autowired
	IManagerService managerService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("name") != null && !"".equals(bodyInfo.get("name"))) {
			para.put("name", bodyInfo.getString("name"));
		}
		
		Mall mall = mallService.selectByMallUuid(manager.getMallUuid()); 
		if(mall != null && !"商盟惠购".equals(mall.getMallName())){
			para.put("mallUuid", mall.getMallUuid());
		}
		
		List<Manager> managerListAll = managerService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<Manager> managerList = managerService.selectByPara(para);
		
		if(managerList.size() <= 0){
			json.put("result", "1");
			json.put("description", "账户列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(Manager m:managerList){
			Map<String,String> map = new HashMap<>();
			Mall orderMall = mallService.selectByMallUuid(m.getMallUuid());
			
			map.put("id", m.getId()+"");
			map.put("mallName", orderMall.getMallName()+"商城");
			map.put("name", m.getName()+"");
			map.put("pwd", m.getPwd()+"");
			map.put("createTime", m.getCreateTime()==null?"":sdf.format(m.getCreateTime()));
			list.add(map);
		}
		
		session.setAttribute("ORDERLIST", list);
		
    	json.put("result", "0");
		json.put("description", "获取账户列表成功");
		json.put("list", list);
		json.put("count", managerListAll.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("managerId") == null || "".equals(bodyInfo.get("managerId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Manager m = managerService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("managerId")));
		
		if(m == null){
			json.put("result", "1");
			json.put("description", "账号不存在");
			return buildReqJsonObject(json);
		}
		
		Map<String,String> map = new HashMap<>();
		Mall orderMall = mallService.selectByMallUuid(m.getMallUuid());
		
		map.put("managerId", m.getId()+"");
		map.put("mallName", orderMall.getMallName()+"商城");
		map.put("name", m.getName()+"");
		map.put("pwd", m.getPwd()+"");
		map.put("createTime", m.getCreateTime()==null?"":sdf.format(m.getCreateTime()));
		
    	json.put("result", "0");
		json.put("description", "获取账号成功");
		json.put("manager", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws UnsupportedEncodingException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("managerId") == null || "".equals(bodyInfo.get("managerId"))) {
			right = 0;
		}else if(bodyInfo.get("name") == null || "".equals(bodyInfo.get("name"))){
			right = 0;
		}else if(bodyInfo.get("pwd") == null || "".equals(bodyInfo.get("pwd"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Manager m = managerService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("managerId")));
		
		if(m == null){
			json.put("result", "1");
			json.put("description", "账号不存在");
			return buildReqJsonObject(json);
		}
		if(manager.getEnabled() != 1 && m.getMallUuid() != manager.getMallUuid()){
			json.put("result", "1");
			json.put("description", "无修改权限");
			return buildReqJsonObject(json);
		}
		
		String pwd = bodyInfo.getString("pwd");
		pwd = CommonUtil.strToMD5(pwd + bodyInfo.getString("name"));
		
		m.setName(bodyInfo.getString("name"));
		m.setPwd(pwd);
		
		Integer rs = managerService.updateByPrimaryKeySelective(m);
		if(rs != 1){
			json.put("result", "0");
			json.put("description", "修改账号失败");
		}else{
			json.put("result", "0");
			json.put("description", "修改账号成功");
		}
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() throws UnsupportedEncodingException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("mallUuid") == null || "".equals(bodyInfo.get("mallUuid"))) {
			right = 0;
		}else if(bodyInfo.get("name") == null || "".equals(bodyInfo.get("name"))){
			right = 0;
		}else if(bodyInfo.get("pwd") == null || "".equals(bodyInfo.get("pwd"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		
		if(manager.getEnabled() != 1){
			json.put("result", "1");
			json.put("description", "无创建权限");
			return buildReqJsonObject(json);
		}
		
		Mall mall = mallService.selectByMallUuid(bodyInfo.getString("mallUuid"));
		
		if(mall == null){
			json.put("result", "1");
			json.put("description", "所属商城不存在");
			return buildReqJsonObject(json);
		}
		
		Manager m = managerService.selectByMallUuid(mall.getMallUuid());
		
		if(m != null){
			json.put("result", "1");
			json.put("description", "所属商城已存在后台账号");
			return buildReqJsonObject(json);
		}
		
		m = new Manager();
		
		String pwd = bodyInfo.getString("pwd");
		pwd = CommonUtil.strToMD5(pwd + bodyInfo.getString("name"));
		
		m.setName(bodyInfo.getString("name"));
		m.setPwd(pwd);
		m.setMallUuid(mall.getMallUuid());
		m.setEnabled((byte)0);
		m.setCreateTime(new Date());
		Integer rs = managerService.insertSelective(m);
		if(rs != 1){
			json.put("result", "0");
			json.put("description", "创建账号失败");
		}else{
			json.put("result", "0");
			json.put("description", "创建账号成功");
		}
		return buildReqJsonObject(json);
	}
}
